/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: property.cpp
 *
 * Purpose: property implementation for base library
 *
 * Developer:
 *   wen.gu , 2019-10-16
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "panda/sys/property.h"
#include "panda/sys/internal/property_impl.h"
 
namespace panda
{
namespace sys
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define PROP_SET_DEF(_type)\
bool PropertySet(const std::string& key, _type val){ \
    return PropertyImpl::instance().set(key, val);    \
}

#define PROP_GET_DEF(_type)\
bool PropertyGet(const std::string& key, _type& val){ \
    return PropertyImpl::instance().get(key, val);    \
}

#define PROP_GETOR_DEF(_type)\
_type PropertyGetOr(const std::string& key, _type defVal){ \
    return PropertyImpl::instance().getOr(key, defVal);    \
}
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/


 using namespace panda::sys::internal_impl;
/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
/**api implementation for set */
PROP_SET_DEF(const std::string&)
PROP_SET_DEF(int8_t)
PROP_SET_DEF(int16_t)
PROP_SET_DEF(int32_t)
PROP_SET_DEF(int64_t)
PROP_SET_DEF(uint8_t)
PROP_SET_DEF(uint16_t)
PROP_SET_DEF(uint32_t)
PROP_SET_DEF(uint64_t)
PROP_SET_DEF(float)
PROP_SET_DEF(double)

/**api implementation for get */
PROP_GET_DEF(std::string)
PROP_GET_DEF(int8_t)
PROP_GET_DEF(int16_t)
PROP_GET_DEF(int32_t)
PROP_GET_DEF(int64_t)
PROP_GET_DEF(uint8_t)
PROP_GET_DEF(uint16_t)
PROP_GET_DEF(uint32_t)
PROP_GET_DEF(uint64_t)
PROP_GET_DEF(float)
PROP_GET_DEF(double)

/**api implementation for getOr */
PROP_GETOR_DEF(int8_t)
PROP_GETOR_DEF(int16_t)
PROP_GETOR_DEF(int32_t)
PROP_GETOR_DEF(int64_t)
PROP_GETOR_DEF(uint8_t)
PROP_GETOR_DEF(uint16_t)
PROP_GETOR_DEF(uint32_t)
PROP_GETOR_DEF(uint64_t)
PROP_GETOR_DEF(float)
PROP_GETOR_DEF(double)


std::string PropertyGetOr(const std::string& key, const std::string& defVal)
{
    return PropertyImpl::instance().getOr(key, defVal);
}

bool PropertyForeach(void (*propFunc)(const std::string& key,
                     const std::string& val, void* opaque),
                     void* opaque)
{
    return PropertyImpl::instance().foreach(propFunc, opaque);
}

bool PropertyGetList(PropertyList& proplist)
{
    return PropertyImpl::instance().getPropertyList(proplist);
}

/**
 * wait until the property change to expect value or time out.
 * timeoutMs: timeout duration, -1: wait for ever, >= 0: wait duration
 *
 */
bool PropertyWaitForProperty(const std::string& key,
                             const std::string& expectVal,
                             int32_t timeoutMs /*= -1*/)
{
    return PropertyImpl::instance().waitForProperty(key, expectVal, timeoutMs);
}

/**
 * wait until the property creation or time out
 * timeoutMs: timeout duration, -1: wait for ever, >= 0: wait duration
 */
bool PropertyWaitForPropertyCreation(const std::string& key,
                                     int32_t timeoutMs /*= -1*/)
{
    return PropertyImpl::instance().waitForPropertyCreation(key, timeoutMs);
}

/**
 * wait until the property changed(don't care about change to what) or time out
 * timeoutMs: timeout duration, -1: wait for ever, >= 0: wait duration
 */
bool PropertyWaitForPropertyChange(const std::string& key,
                                   int32_t timeoutMs /*= -1*/)
{
    return PropertyImpl::instance().waitForPropertyChange(key, timeoutMs);
}

} /** namespace sys */
} /** namespace panda */
